package com.daisuxia.task;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.daisuxia.back.dao.ISendMoneyStatisticDao;
import com.daisuxia.web.pojo.BorrowOrder;
import com.daisuxia.web.service.IBorrowStatisticsReportService;
import com.daisuxia.web.util.SendSmsUtil;

import redis.clients.jedis.JedisCluster;

/**
 * 警告任务
 * @author zhangsh Tel:13817876783
 * @date 2017年9月20日 下午2:05:48
 * @Title: RiskManageTaskService
 * @ClassName: RiskManageTaskService
 * @Description:
 */

public class AlarmManageTaskService {
	private static Logger logger = LoggerFactory.getLogger(AlarmManageTaskService.class);
	@Autowired
	JedisCluster jedisCluster;
	@Autowired
	private ISendMoneyStatisticDao sendMoneyStatisticDao;
	@Autowired
	private IBorrowStatisticsReportService borrowStatisticsReportService;
	
	
	
	public void sendAlarmMsg() {
		logger.error("begin judge sendAlarmMsg=====");
		try {

			Thread t = new Thread(new Runnable() {
				@Override
				public void run() {

					loaningCountAlarm();
					jsTgvAlarm();
					djsCountAlarm();

				}
			});
			t.start();
		} catch (Exception e) {
			logger.error("sendAlarmMsg error 预警提醒  错误！", e);
		}

	}
	
	/**
	 * 打款中订单每天累计5次超过500单，发送提醒
	 */
	public void loaningCountAlarm() {
		try {

			String keyloaning = "keyloaning";
			String keyalarmFlag = "keyalarmFlag";
			Boolean loaningFlag = true;// 是否发送短信
			int loaningAlarmSum = 0;// 统计次数
			int alarmCount = 500;// 判断限制
			if (jedisCluster.get(keyloaning) != null) {
				loaningAlarmSum = Integer.parseInt(jedisCluster.get(keyloaning));
				loaningFlag = Boolean.parseBoolean(jedisCluster.get(keyalarmFlag));
			} else {
				jedisCluster.setex(keyloaning, BorrowOrder.ALARM_ONEDAY, "0");
				jedisCluster.setex(keyalarmFlag, BorrowOrder.ALARM_ONEDAY, "true");
			}

			if (loaningFlag) {
				// 放款中笔数超过多少5次发短信
				Map<String, Object> map = sendMoneyStatisticDao.findLoaning();
				if (map != null && map.size() > 0) {
					int loaningCount = Integer.parseInt(String.valueOf(map.get("loaningCount")));
					if (loaningCount > alarmCount) {
						loaningAlarmSum += 1;
						jedisCluster.set(keyloaning, String.valueOf(loaningAlarmSum));
					}
					if (loaningAlarmSum > 4 && loaningFlag) {
						loaningFlag = false;
						jedisCluster.set(keyloaning, "0");
						jedisCluster.set(keyalarmFlag, "false");
						SendSmsUtil.sendSmsDiyCL("13817876783",
								"当前放款中订单超过：" + alarmCount + "单,请相关人员及时确认，确认后记得重新打开提醒功能！");
						SendSmsUtil.sendSmsDiyCL("13817876783",
								"陈总，您好！当前放款中订单超过：" + alarmCount + "单,请相关人员及时确认，确认后记得重新打开提醒功能！");
						SendSmsUtil.sendSmsDiyCL("13817876783",
								"蔡总，您好！当前放款中订单超过：" + alarmCount + "单,请相关人员及时确认，确认后记得重新打开提醒功能！");

						logger.error("已发送放款中提醒短信");
						logger.error(" alarm  alarm  loaning order  借款订单超过500需要核实处理.......zjb");
					}
				}
			}
		} catch (Exception e) {
			logger.error("loaningCountAlarm error ", e);
		}
	}
	
	/**
	 * 机审通过率每天累计5次超过30%，发送提醒
	 */
	public void jsTgvAlarm() {
		try {

			String keyJsTgv = "keyJsTgv";
			String keyJsTgvalarmFlag = "keyJsTgvFlag";
			Boolean jsTgvFlag = true;// 是否发送短信
			int loaningAlarmSum = 0;// 统计次数
			BigDecimal alarmCount = new BigDecimal(30);// 判断限制
			if (jedisCluster.get(keyJsTgv) != null) {
				loaningAlarmSum = Integer.parseInt(jedisCluster.get(keyJsTgv));
				jsTgvFlag = Boolean.parseBoolean(jedisCluster.get(keyJsTgvalarmFlag));
			} else {
				jedisCluster.setex(keyJsTgv, BorrowOrder.ALARM_ONEDAY, "0");
				jedisCluster.setex(keyJsTgvalarmFlag, BorrowOrder.ALARM_ONEDAY, "true");
			}

			if (jsTgvFlag) {

				Map<String, Object> params = new HashMap<String, Object>();
				List<Integer> list1 = sendMoneyStatisticDao.findRiskOrdersToday();
				if (list1 != null && list1.size() > 0) {

					Integer riskOrdersTotalToday = list1.get(0);
					Integer riskOrdersPassToday = list1.get(1);
					BigDecimal riskOrdersPassRateToday = BigDecimal.ZERO;
					if (riskOrdersTotalToday != null && riskOrdersTotalToday.intValue() != 0) {
						riskOrdersPassRateToday = new BigDecimal(riskOrdersPassToday).multiply(new BigDecimal(100))
								.divide(new BigDecimal(riskOrdersTotalToday), 2, BigDecimal.ROUND_HALF_UP);
					}

					if (riskOrdersPassRateToday.compareTo(alarmCount) == 1) {
						loaningAlarmSum += 1;
						jedisCluster.set(keyJsTgv, String.valueOf(loaningAlarmSum));
					}
					if (loaningAlarmSum > 4 && jsTgvFlag) {
						jsTgvFlag = false;
						jedisCluster.set(keyJsTgv, "0");
						jedisCluster.set(keyJsTgvalarmFlag, "false");
						SendSmsUtil.sendSmsDiyCL("13817876783",
								"当前机审通过率超过：" + alarmCount + "%,请相关人员尽快查看确认，确认后记得重新打开提醒功能！");
						SendSmsUtil.sendSmsDiyCL("13817876783",
								"陈总，您好！当前机审通过率超过：" + alarmCount + "%,请相关人员尽快查看确认，确认后记得重新打开提醒功能！");
						SendSmsUtil.sendSmsDiyCL("13817876783",
								"蔡总，您好！当前机审通过率超过：" + alarmCount + "%,请相关人员尽快查看确认，确认后记得重新打开提醒功能！");
						SendSmsUtil.sendSmsDiyCL("13817876783",
								"当前机审通过率超过：" + alarmCount + "%,请相关人员尽快查看确认，确认后记得重新打开提醒功能！");

						logger.error("已发送机审通过率提醒短信");
						logger.error("   alarm  jsTgvAlarm    当前机审通过率超过30%需要核实处理.......fyc");
					}
				}
			}
		} catch (Exception e) {
			logger.error("jsTgvAlarm error ", e);
		}
	}

	/**
	 * 待机审订单每天累计5次超过500单，发送提醒
	 */
	public void djsCountAlarm() {
		try {

			String keyloaning = "keyDjsCount";
			String keyalarmFlag = "keyDjsCountFlag";
			Boolean loaningFlag = true;// 是否发送短信
			int loaningAlarmSum = 0;// 统计次数
			int alarmCount = 500;// 判断限制
			if (jedisCluster.get(keyloaning) != null) {
				loaningAlarmSum = Integer.parseInt(jedisCluster.get(keyloaning));
				loaningFlag = Boolean.parseBoolean(jedisCluster.get(keyalarmFlag));
			} else {
				jedisCluster.setex(keyloaning, BorrowOrder.ALARM_ONEDAY, "0");
				jedisCluster.setex(keyalarmFlag, BorrowOrder.ALARM_ONEDAY, "true");
			}

			if (loaningFlag) {
				// 放款中笔数超过多少5次发短信
				List<Integer> list1 = sendMoneyStatisticDao.findDjsOrdersToday();
				if (list1 != null && list1.size() > 0) {
					int loaningCount = list1.get(0);
					;
					if (loaningCount > alarmCount) {
						loaningAlarmSum += 1;
						jedisCluster.set(keyloaning, String.valueOf(loaningAlarmSum));
					}
					if (loaningAlarmSum > 4 && loaningFlag) {
						loaningFlag = false;
						jedisCluster.set(keyloaning, "0");
						jedisCluster.set(keyalarmFlag, "false");

						SendSmsUtil.sendSmsDiyCL("13817876783",
								"当前待机审订单超过：" + alarmCount + "单,请相关人员尽快查看确认，确认后记得重新打开提醒功能！");
						SendSmsUtil.sendSmsDiyCL("13817876783",
								"陈总，您好！当前待机审订单超过：" + alarmCount + "单,请相关人员尽快查看确认，确认后记得重新打开提醒功能！");
						SendSmsUtil.sendSmsDiyCL("13817876783",
								"蔡总，您好！当前待机审订单超过：" + alarmCount + "单,请相关人员尽快查看确认，确认后记得重新打开提醒功能！");
						logger.error("已发送djsCountAlarm提醒短信");
					}
				}
			} else {
				logger.error(" djsCountAlarm   待机审订单当天累计5次超过500单，发送提醒.......fyc");
			}
		} catch (Exception e) {
			logger.error("djsCountAlarm error", e);
		}
	}
	
	public void createBorrowStaticsReport() {
		try {
			borrowStatisticsReportService.createborrowStaticReportDateByDate(null);
		} catch (Exception e) {
			logger.error("createBorrowStaticsReport error", e);
		}

	}
	
}